/**	
 * Author : Mht
 * Desc : For 2016 RDM 
 */
package configuration;

import java.util.Properties;

import javax.annotation.Resource;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import common.PropertyNames;
import lombok.extern.log4j.Log4j;
import schedule.listener.ScheduleJobListener;

/**
 * @author Mht
 * @date Jun 3, 2016
 */
@Log4j
@Configuration
@PropertySource(value = { "classpath:conf/quartz.properties" })
public class QuartzConfiguration {

	public QuartzConfiguration() {
		log.info("[Initialize Quartz Configuration]");
	}

	@Resource
	private Environment env;

	@Resource(name = "scheduleJobListener")
	private ScheduleJobListener scheduleJobListener;

	@Bean(name = "stdSchedulerFactory")
	public StdSchedulerFactory stdSchedulerFactory() {
		try {
			StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
			stdSchedulerFactory.initialize(schedulerProperties());
			return stdSchedulerFactory;
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			log.error("Initialize StdScheduler Factory Error!", e);
			return null;
		}
	}

	@Bean(name = "scheduler")
	public Scheduler scheduler() {
		try {
			Scheduler scheduler = stdSchedulerFactory().getScheduler();
			//注册全局的定时任务监听器
			scheduler.getListenerManager().addSchedulerListener(scheduleJobListener);
			//开启定时任务
			scheduler.start();
			return scheduler;
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			log.error("Initialize Scheduler Error!", e);
			return null;
		}
	}

	@SuppressWarnings("serial")
	Properties schedulerProperties() {

		return new Properties() {

			{
				setProperty("org.quartz.scheduler.rmi.export", env.getProperty(PropertyNames.QUARTZ_EXPORT));
				setProperty("org.quartz.scheduler.rmi.proxy", env.getProperty(PropertyNames.QUARTZ_PROXY));
				setProperty("org.quartz.threadPool.class", env.getProperty(PropertyNames.QUARTZ_CLASS));
				setProperty("org.quartz.threadPool.threadCount", env.getProperty(PropertyNames.QUARTZ_THREAD_COUNT));
				setProperty("org.quartz.threadPool.threadPriority",
						env.getProperty(PropertyNames.QUARTZ_THREAD_PRIORITY));
//				setProperty("org.quartz.jobStore.useProperties", env.getProperty(PropertyNames.QUARTZ_USE_PROPERTIES));
				setProperty("org.quartz.jobStore.class", env.getProperty(PropertyNames.QUARTZ_JOBSTORE_CLASS));
				setProperty("org.quartz.jobStore.tablePrefix", env.getProperty(PropertyNames.QUARTZ_JOBSTORE_PREFIX));
				setProperty("org.quartz.jobStore.driverDelegateClass",
						env.getProperty(PropertyNames.QUARTZ_JOBSTORE_DELEGATE));
				setProperty("org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread",
						env.getProperty(PropertyNames.QUARTZ_THREAD_LOADER));

				setProperty(PropertyNames.DB_DS_NAME, env.getProperty(PropertyNames.DB_DS_NAME));
				setProperty(PropertyNames.DB_DRIVER, env.getProperty(PropertyNames.DB_DRIVER));
				setProperty(PropertyNames.DB_URL, env.getProperty(PropertyNames.DB_URL));
				setProperty(PropertyNames.DB_USER, env.getProperty(PropertyNames.DB_USER));
				setProperty(PropertyNames.DB_PWD, env.getProperty(PropertyNames.DB_PWD));
			}
		};
	}
}
